package com.bs.dao.impl;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bs.dao.BaseDao;
import com.bs.dao.MedicineInfoDao;
import com.bs.entity.MedicineInfo;

/**
 * 药品信息dao层处理类
 * @author shimeimei
 *
 */

public class MedicineInfoDaoImpl  extends BaseDao implements MedicineInfoDao {
	
	
	/**
	 * 保存药品信息
	 */
    @Transactional(propagation=Propagation.REQUIRED)
	public boolean addMedicineInfo(MedicineInfo medicineInfo) {
    	System.out.println("------------dao");
    	boolean flag =false;
		try {
			hibernateTemplate.save(medicineInfo);
			System.out.println("--------dao over");
			flag =true;
		} catch (Exception e) {
		 e.printStackTrace();
		}
		System.out.println("flg:"+flag);
		return flag;
	}
   
    /**
     *获取所有药品信息
     */
	public List<MedicineInfo> getMedicineInfoList() {
		String hql ="from MedicineInfo";
		List medicineInfo =null;
		try {
		medicineInfo =hibernateTemplate.find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return medicineInfo;
	}
    
	/**
	 * 获取指定药品信息
	 */
	public List<MedicineInfo> getMedicineList(String keyWord) {
		
		String hql="from MedicineInfo m  left join fetch m.medicineCategory where m.medicineName like '%"+
				keyWord+"%'";
		List<MedicineInfo> list =null;
		String key ="%"+keyWord+"%";
		try{
			list  =hibernateTemplate.find(hql);
		}catch (Exception e) {
			e.printStackTrace();
			
		}
		
		return list;
	}
    
	/**
	 * 获取快速匹配的键值
	 */
	public List<MedicineInfo> getKeyWord(String keyWord) {
	    String hql="select m.medicineName from MedicineInfo m where m.medicineName like'"+keyWord+"%'";
	    List<MedicineInfo>  list =null;
	    try{
	    	list =hibernateTemplate.find(hql);
	    }catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public MedicineInfo getMedicineInfoById(String id) {
		String hql="from MedicineInfo where medicineId=?";
		MedicineInfo medicineInfo =null;
		try {
			medicineInfo =(MedicineInfo)hibernateTemplate.find(hql, new Object[]{Long.parseLong(id)}).get(0);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return medicineInfo;
	}
	
	/**
	 * 更新药品信息
	 */
  @Transactional(propagation=Propagation.REQUIRED)
	public boolean updateMedicine(MedicineInfo medicineInfo) {
	  boolean flag =false;
	 try{
		 hibernateTemplate.update(medicineInfo);
		 flag=true;
		 
	 }catch (Exception e) {
		 e.printStackTrace();
		 
		
	}
		
		return flag;
	}
}
